package hjt.com.module.user.service;

import android.content.Context;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.alibaba.android.arouter.launcher.ARouter;

import hjt.com.componentbase.constant.ARouterConstants;
import hjt.com.componentbase.service.ServiceFactory;

/**
 * Created by hjt on 2019/5/5.
 */
@Interceptor(priority = 8, name = "登录状态拦截器")
public class LoginInterceptor implements IInterceptor {
    private Context context;

    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        // onContinue 和 onInterrupt 至少需要调用其中一种，否则不会继续路由

        if (postcard.getPath().equals(ARouterConstants.ACTIVITY_DESIGN_MAIN)) {
            if (ServiceFactory.getInstance().getLoginService().isLogin()) {
                callback.onContinue(postcard); // 处理完成，交还控制权
            } else {
                //没有登录,注意需要传入context
                ARouter.getInstance().build(ARouterConstants.ACTIVITY_USER_SECOND).navigation(context);
            }
        } else {
            callback.onContinue(postcard);
        }
    }

    @Override
    public void init(Context context) {
        //拦截器的初始化，会在sdk初始化的时候调用一次，仅会调用一次
        this.context = context;
    }
}
